package com.anda.logistics.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.anda.logistics.dao.DriverLocationDao;
import com.anda.logistics.dao.TruckInfoDao;
import com.anda.logistics.map.util.GoogleGeocode;
import com.anda.logistics.model.DriverLocation;
import com.anda.logistics.model.DriverLocationRt;
import com.anda.logistics.model.TruckInfo;
import com.anda.logistics.service.DriverLocationService;
import com.anda.logistics.util.JQGridPager;
import com.anda.logistics.vo.LocationStatVo;

@Component
public class DriverLocationServiceImpl implements DriverLocationService{
	
	private DriverLocationDao driverLocationDao;
	private TruckInfoDao truckInfoDao;
	
	@Override
	public void save(DriverLocation location) {				
		TruckInfo ti = truckInfoDao.getOneByMobileNo(location.getNumber());
		if (ti != null){
			
			if (location.getCity() == null){
				try {
				DriverLocation tmp = GoogleGeocode.getLocationByLatLng(location.getLat(), location.getLng());
				if (tmp != null){
					location.setAccessToken(tmp.getAccessToken());
					location.setCity(tmp.getCity());
					location.setCountry(tmp.getCountry());
					location.setCountryCode(tmp.getCountryCode());
					location.setRegion(tmp.getRegion());
					location.setStreet(tmp.getStreet());
					location.setStreetNumber(tmp.getStreetNumber());
				}
				} catch (Exception e){					
				}
			}
			
			DriverLocationRt rt = new DriverLocationRt();
			rt.setAccessToken(location.getAccessToken());
			rt.setAccuracy(location.getAccuracy());
			rt.setCity(location.getCity());
			rt.setCountry(location.getCountry());
			rt.setCountryCode(location.getCountryCode());
			rt.setGetTime(location.getGetTime());
			rt.setId(ti.getId());
			rt.setLat(location.getLat());
			rt.setLng(location.getLng());
			rt.setNumber(location.getNumber());
			rt.setRegion(location.getRegion());
			rt.setStreet(location.getStreet());
			rt.setStreetNumber(location.getStreetNumber());
			rt.setType(location.getType());
			rt.setUpTime(location.getUpTime());
			
			rt.setAddressDetail( location.getAddressDetail());
			rt.setCityCode(location.getCityCode());
			rt.setFeatureName(location.getFeatureName());
			rt.setLocationDesc(location.getLocationDesc());
			rt.setSubAdminArea( location.getSubAdminArea());
			rt.setSubLocality(location.getSubLocality());
			rt.setThoroughfare(location.getThoroughfare());
			driverLocationDao.save(location);
			driverLocationDao.deleteRt(ti.getId());
			driverLocationDao.save(rt);
		}
	}	
	
	public DriverLocationDao getDriverLocationDao() {
		return driverLocationDao;
	}
	@Resource
	public void setDriverLocationDao(DriverLocationDao driverLocationDao) {
		this.driverLocationDao = driverLocationDao;
	}

	public TruckInfoDao getTruckInfoDao() {
		return truckInfoDao;
	}
	@Resource
	public void setTruckInfoDao(TruckInfoDao truckInfoDao) {
		this.truckInfoDao = truckInfoDao;
	}

	@Override
	public DriverLocationRt getLocationByMobileNo(String mobileNo) {
		return driverLocationDao.getLocationByMobileNo(mobileNo);
	}

	@Override
	public List<LocationStatVo> getStat(int level, String plateNo, String region) {		
		return driverLocationDao.getStat(level, plateNo, region);
	}

	@Override
	public List<TruckInfo> getStat(String plateNo, String region, String city) {
		return driverLocationDao.getStat(plateNo, region, city);
	}

	@Override
	public List<TruckInfo> getPagerTruckList(JQGridPager pager, String city,
			String region, String plateNo) {
		return driverLocationDao.getPagerTruckList( pager,  city,
				 region,  plateNo);
	}

	@Override
	public long getPagerTruckRecords(JQGridPager pager, String city,
			String region, String plateNo) {
		return driverLocationDao.getPagerTruckRecords( pager,  city,
				 region,  plateNo);
	}


}
